#pragma once

#include "MatchInterface.h"
#include "Vectors.h"

enum EShotType
{
	ShotNormal,
	ShotFreekick,
	ShotPenaltykick
};

class Team;
class ServiceProvider;

class OldMatch : public MatchInterface
{
public:
	OldMatch(ServiceProvider* i_sp);

	virtual void PlayMatch(const bool i_produceEvents);

private:
	virtual void InnerInit(void* i_innerParams);

	// begin of the match
	void	PreMatchEvents();
	void	TeamsCondition(size_t teamIndex);

	// during match
	void	PlayHalfMatch(const int i_startingMinute, const bool i_produceEvents);
	void	CheckInMatchInjury();
	void	CheckPenalty(const int i_minute, const size_t teamIndex, const bool i_produceEvents);
	void	CheckForfait(int& io_minute);
	void	UpdateStaminas(const int i_minute, const bool i_produceEvents);
	void	UpdateStamina(const int i_team, const int i_player, const int i_minute, const bool i_produceEvents, const int i_toBeAdded);
	void	UpdateEval(const int i_team, const int i_player, const int i_howMuch, const bool i_plus);
	void	HalfTime(const bool i_produceEvents);
	void	CalcActualSkills(const int i_team, const int i_player);
	bool	CalcActualSkillsUnsafe(const int i_team, const int i_player);
	void	RecalcSums(const int i_team);
	int		GetConditionPercent(const int i_team, const int i_player) const;
	
	// shot & actions
	int		CheckActions(const int i_minute, const bool i_produceEvents);
	void	NotShot();
	int		Action(const int i_minute, const size_t i_team, const bool i_produceEvents);
	bool	DirectShot(const int i_minute, const size_t i_team, const bool i_produceEvents);
	bool	FreeKick(const int i_minute, const size_t i_team, const bool i_produceEvents);
	bool	PenaltyKick(const int i_minute, const size_t i_team, const bool i_produceEvents);
	int		SelectShootingPlayer(const int i_team) const;
	int		SelectDefendingPlayer(const int i_team) const;
	bool	ShotPlayer(const int i_minute, const size_t i_team, const int i_shootingPlayer, const int i_shotType, const bool i_produceEvents);
	void	CalcShotEval(const int i_team, const int i_shootingPlayer, const int i_attackResult, const int i_saveResult);
	void	CalcActionEval(const int i_atkTeam, const int i_defTeam, const int i_atkIndex, 
				const int i_defIndex, const int i_atkResult, const int i_defResult);

	// end of the match
	void	CalcEvaluations();
	void	PostMatchPlayerUpdate(const bool i_produceEvents);
	void	CalcWinEval();

	//misc events
	void	EvaluationsEvents(const int i_minute);
	void	HalfTimeEvents();
	void	PostMatchEvents();
	void	ExtraTimeEvents(const int i_minute, const int i_extraTime);
	void	ShotEvents(const int i_minute, const int i_team, const int i_shootingPlayer, const int i_attackResult, const int i_saveResult, const int i_shotType);
	void	ActionEvents(const int i_minute, const int i_atkTeam, const int i_defTeam, 
				const int i_atkIndex, const int i_defIndex, const int i_atkResult, const int i_defResult);

	// string manipulation
	const STL::string& CurrentResult(STL::string& o_str) const;
	const STL::string& HomeAwayString(const int i_team) const;
	const STL::string& GetPlayerDecoratedName(const int i_team, const int i_player, STL::string& o_str) const;

	struct OldMatchTeamData
	{
		IntVec m_Plus; //used for evaluations
		IntVec m_Minus; //used for evaluations

		IntVec m_shoots;
		IntVec m_atk_total;
		IntVec m_atk_won;
		IntVec m_def_total;
		IntVec m_def_won;
		IntVec m_fouls;

		int m_team_shoots;
		int m_team_atk_total;
		int m_team_atk_won;
		int m_team_def_total;
		int m_team_def_won;
		int m_team_fouls;

		IntVec m_conditions;

		IntVec m_BaseAttackSkills;
		IntVec m_BaseRoleAttackFactor;
		IntVec m_AttackSkills;
		IntVec m_RoleAttackFactor;
		IntVec m_InvRoleAttackFactor;
		int m_RoleAttackFactorSum;
		int m_InvRoleAttackFactorSum;

		IntVec m_BaseDefenseSkills;
		IntVec m_BaseRoleDefenseFactor;
		IntVec m_DefenseSkills;
		IntVec m_RoleDefenseFactor;
		IntVec m_InvRoleDefenseFactor;
		int m_RoleDefenseFactorSum;
		int m_RoleDefenseSelectorSum;
		int m_InvRoleDefenseFactorSum;

		IntVec m_roles;
		int m_formation;

		int m_gkBaseSkill;
		int m_gkSkill;

		IntVec m_Aggressivity;
		int m_AggSum;

		const Team* m_team;
	};

	static const int s_shotThreshold = 50; // move in settings
	static const int s_baseAttackFactor = 50; // move in settings
	static const int s_baseHomeAttackFactor = 30; // move in settings
	static const int s_homeAttackFactor = s_baseAttackFactor + s_baseHomeAttackFactor; //move it in init
	static const int s_awayAttackFactor = s_baseAttackFactor; //move it in init
	OldMatchTeamData m_data[2];

	//cache in order to avoid a lot of string allocations
	STL::string m_tempString; 
	STL::string m_stringTemp;

	int m_matchDuration;
	unsigned int m_numberOfTitolars;

	enum
	{
		NONE = 0,
		ACTION,
		SHOT,
		GOAL,
		NUM_ACTION_EVENTS
	};
};

